package xiang.weiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xiang.weiyu.pojo.*;
import xiang.weiyu.service.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/statics")
public class StaticsController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 菜系营收占比分析
     *
     * @return
     */
    @GetMapping("/dishCash")
    public List<DishCategoryCash> dishCash() {
        //返回的数据实体类
        List<DishCategoryCash> dccList = new ArrayList<>();

        //1.获取category表中所有type为1的菜品类型
        LambdaQueryWrapper<Category> qwCategory = new LambdaQueryWrapper<>();
        qwCategory.eq(Category::getType, 1);
        List<Category> categoryList = categoryService.list(qwCategory);

        //2.遍历集合,将每一个菜系名称设置进dccList集合中
        for (Category category : categoryList) {
            DishCategoryCash dcc = new DishCategoryCash();
            dcc.setName(category.getName());//菜系的名称
            dccList.add(dcc);
        }

        //3.获取订单详细中的所有的菜品数据
        LambdaQueryWrapper<OrderDetail> qwOrderDetail = new LambdaQueryWrapper<>();
        qwOrderDetail.isNotNull(OrderDetail::getDishId);
        List<OrderDetail> orderDetailList = orderDetailService.list(qwOrderDetail);
        for (OrderDetail orderDetail : orderDetailList) {
            BigDecimal amount = orderDetail.getAmount();//金额

            //4.根据菜品id查询到Dish
            Long dishId = orderDetail.getDishId();//菜品id
            Dish dish = dishService.getByDishId(dishId);
            //5.根据分类id查询到category
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);

            //6.遍历封装类集合,将amount赋值给value
            for (DishCategoryCash dishCategoryCash : dccList) {
                //判断如果菜系名相同,封装类中的value值+=上amount金额
                if (category.getName().equals(dishCategoryCash.getName())) {
                    BigDecimal a1 = dishCategoryCash.getValue();
                    if (a1 != null) {
                        amount = amount.add(a1);
                    }
                    dishCategoryCash.setValue(amount);
                }
            }
        }

        return dccList;
    }

    /**
     * 菜系营收占比分析
     *
     * @return
     */
    @GetMapping("/setmealCash")
    public List<SetmealCash> setmealCash() {
        //返回的数据实体类
        List<SetmealCash> setmealCashs = new ArrayList<>();

        //1.获取category表中所有type为2的套餐类型
        LambdaQueryWrapper<Category> qwCategory = new LambdaQueryWrapper<>();
        qwCategory.eq(Category::getType, 2);
        List<Category> categoryList = categoryService.list(qwCategory);

        //2.遍历集合,将每一个菜系名称设置进dccList集合中
        for (Category category : categoryList) {
            SetmealCash dcc = new SetmealCash();
            dcc.setName(category.getName());//菜系的名称
            setmealCashs.add(dcc);
        }

        //3.获取订单详细中的所有的菜品数据
        LambdaQueryWrapper<OrderDetail> qwOrderDetail = new LambdaQueryWrapper<>();
        qwOrderDetail.isNotNull(OrderDetail::getSetmealId);
        List<OrderDetail> orderDetailList = orderDetailService.list(qwOrderDetail);
        for (OrderDetail orderDetail : orderDetailList) {
            BigDecimal amount = orderDetail.getAmount();//金额

            //4.根据菜品id查询到Dish
            Long dishId = orderDetail.getSetmealId();//套餐id
            Setmeal setmeal = setmealService.getById(dishId);
            //5.根据分类id查询到category
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);

            //6.遍历封装类集合,将amount赋值给value
            for (SetmealCash setmealCash : setmealCashs) {
                //判断如果菜系名相同,封装类中的value值+=上amount金额
                if (category.getName().equals(setmealCash.getName())) {
                    BigDecimal a1 = setmealCash.getValue();
                    if (a1 != null) {
                        amount = amount.add(a1);
                    }
                    setmealCash.setValue(amount);
                }
            }
        }

        return setmealCashs;
    }

    /**
     * 菜系销量占比分析
     *
     * @return
     */
    @GetMapping("/dishNum")
    public List<DishNumCash> dishNum() {
        //返回的数据实体类
        List<DishNumCash> dishNumCashs = new ArrayList<>();

        //1.获取category表中所有type为1的菜品类型
        LambdaQueryWrapper<Category> qwCategory = new LambdaQueryWrapper<>();
        qwCategory.eq(Category::getType, 1);
        List<Category> categoryList = categoryService.list(qwCategory);

        //2.遍历集合,将每一个菜系名称设置进dccList集合中
        for (Category category : categoryList) {
            DishNumCash dcc = new DishNumCash();
            dcc.setName(category.getName());//菜系的名称
            dishNumCashs.add(dcc);
        }

        //3.获取订单详细中的所有的菜品数据
        LambdaQueryWrapper<OrderDetail> qwOrderDetail = new LambdaQueryWrapper<>();
        qwOrderDetail.isNotNull(OrderDetail::getDishId);
        List<OrderDetail> orderDetailList = orderDetailService.list(qwOrderDetail);
        for (OrderDetail orderDetail : orderDetailList) {

            //4.根据菜品id查询到Dish
            Long dishId = orderDetail.getDishId();//菜品id
            Dish dish = dishService.getByDishId(dishId);
            //5.根据分类id查询到category
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);

            //6.遍历封装类集合,将amount赋值给value
            for (DishNumCash dishCategoryCash : dishNumCashs) {
                //判断如果菜系名相同,封装类中的value值+=上number份数
                if (category.getName().equals(dishCategoryCash.getName())) {
                    Integer number = orderDetail.getNumber();
                    Integer value = dishCategoryCash.getValue();
                    if (value != null) {
                        number += value;
                    }
                    dishCategoryCash.setValue(number);
                }
            }
        }

        return dishNumCashs;
    }

    /**
     * 菜系销量占比分析
     *
     * @return
     */
    @GetMapping("/setmealNum")
    public List<SetmealNumCash> setmealNum() {
        //返回的数据实体类
        List<SetmealNumCash> dishNumCashs = new ArrayList<>();

        //1.获取category表中所有type为1的菜品类型
        LambdaQueryWrapper<Category> qwCategory = new LambdaQueryWrapper<>();
        qwCategory.eq(Category::getType, 2);
        List<Category> categoryList = categoryService.list(qwCategory);

        //2.遍历集合,将每一个菜系名称设置进dccList集合中
        for (Category category : categoryList) {
            SetmealNumCash dcc = new SetmealNumCash();
            dcc.setName(category.getName());//菜系的名称
            dishNumCashs.add(dcc);
        }

        //3.获取订单详细中的所有的菜品数据
        LambdaQueryWrapper<OrderDetail> qwOrderDetail = new LambdaQueryWrapper<>();
        qwOrderDetail.isNotNull(OrderDetail::getSetmealId);
        List<OrderDetail> orderDetailList = orderDetailService.list(qwOrderDetail);
        for (OrderDetail orderDetail : orderDetailList) {

            //4.根据菜品id查询到Dish
            Long setmealId = orderDetail.getSetmealId();//菜品id
            Setmeal setmeal = setmealService.getById(setmealId);
            //5.根据分类id查询到category
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);

            //6.遍历封装类集合,将amount赋值给value
            for (SetmealNumCash dishCategoryCash : dishNumCashs) {
                //判断如果菜系名相同,封装类中的value值+=上number份数
                if (category.getName().equals(dishCategoryCash.getName())) {
                    Integer number = orderDetail.getNumber();
                    Integer value = dishCategoryCash.getValue();
                    if (value != null) {
                        number += value;
                    }
                    dishCategoryCash.setValue(number);
                }
            }
        }

        return dishNumCashs;
    }

    /**
     * 月营业额趋势图
     *
     * @return CashPerMonth
     */
    @GetMapping("cashPerMonth")
    public CashPerMonth cashPerMonth() {
        //自定义的封装实体类
        CashPerMonth cashPerMonth = new CashPerMonth();
        List<String> monthList = new ArrayList<>();
        List<BigDecimal> dataList = new ArrayList<>();

        //获取当前月份的第一天的0点0分0秒
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime monthTime = now.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);

        //往前一年的每个月
        for (int i = 12; i > 0; i--) {

            //每个月的营业额
            BigDecimal monthAmount = new BigDecimal(0);

            //beginTime:上个月  endTime:这个月
            LocalDateTime beginTime = monthTime.minusMonths(i);
            LocalDateTime endTime = monthTime.minusMonths(i - 1);

            //获取全部的orders订单数据,根据order_time查询每个月的营业额
            LambdaQueryWrapper<Orders> qwOrders = new LambdaQueryWrapper<>();
            qwOrders.between(Orders::getOrderTime, beginTime, endTime);
            List<Orders> orders = ordersService.list(qwOrders);

            //遍历订单,根据订单id查询订单详细表数据,加等上amount金额
            for (Orders order : orders) {
                LambdaQueryWrapper<OrderDetail> qwOrderDetail = new LambdaQueryWrapper<>();
                qwOrderDetail.eq(OrderDetail::getOrderId, order.getId());
                List<OrderDetail> orderDetails = orderDetailService.list(qwOrderDetail);
                for (OrderDetail orderDetail : orderDetails) {
                    if (orderDetail.getAmount() != null) {
                        monthAmount = monthAmount.add(orderDetail.getAmount());
                    }
                }
            }

            //向dataList中添加营业额,单位(万元),元除以1万
            dataList.add(monthAmount.divide(new BigDecimal(10000)));
            //monthList中添加对应月份,格式yyyy-MM
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");//-dd HH:mm:ss
            String month = beginTime.format(dateTimeFormatter);
            monthList.add(month);
        }
        cashPerMonth.setMonthList(monthList);
        cashPerMonth.setDatalisttt(dataList);
        return cashPerMonth;
    }
}
